package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.es.EsProd;
import com.bwie.mapper.TbProdMapper;
import com.bwie.pojo.TbProd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/3/29 9:13
 */

@Component
@Slf4j
public class SyncProdTask {

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void syncProdToEs(){
        log.info("开始同步商品到ES");
        //-- 从数据库中读取所有的商品数据
        List<TbProd> tbProds = tbProdMapper.selectList(null);

        //-- 把商品信息存入ES中
        for (TbProd tbProd : tbProds) {
            EsProd esProd = BeanUtil.toBean(tbProd, EsProd.class);
            elasticsearchRestTemplate.save(esProd);
        }
        log.info("同步商品到ES完成");
    }

}
